package cn.selfiot.iotman.core;

import cn.selfiot.iotman.view.ResourceConstants;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.Pane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ConcurrentHashMap;

/**
 * Stage管理器
 *
 * @author hugo
 * @since 2020/7/31
 */
public class StageManager {
    private static final Logger LOGGER = LoggerFactory.getLogger(StageManager.class);
    
    private final ConcurrentHashMap<String, StageController> controllers = new ConcurrentHashMap<>();
    
    private static StageManager INSTANCE;
    
    private StageManager() {
    }
    
    public synchronized static StageManager getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new StageManager();
        }
        return INSTANCE;
    }
    
    
    public void addController(String name, StageController controller) {
        this.controllers.put(name, controller);
    }
    
    public StageController getController(String name, StageParams params) {
        StageController controller = this.controllers.get(name);
        controller.setParams(params);
        return controller;
    }
    
    public StageController getController(String name) {
        return this.controllers.get(name);
    }
    
    
    /**
     * 加载Stage，无缓存方式
     */
    public Stage loadStage(Stage stage, StageController controller,
                           String viewId, String resources,
                           String title, Modality modality) {
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource(resources),
                    I18N.getBundle());
            loader.setController(controller);
            Pane pane = loader.load();
            this.addController(viewId, controller);
            
            Scene scene = new Scene(pane);
            scene.getStylesheets().add(StageManager.class
                    .getResource(ResourceConstants.STYLE_DEFAULT)
                    .toExternalForm());
            stage.setTitle(title);
            stage.getIcons().add(new Image(StageManager.class
                    .getResource(ResourceConstants.ICON_APP).toExternalForm()));
            if (modality == Modality.APPLICATION_MODAL) {
                stage.initStyle(StageStyle.UTILITY);
                stage.setResizable(false);
            }
            if (modality != null) {
                stage.initModality(modality);
            }
            stage.setScene(scene);
            
            return stage;
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error(e.getMessage());
        }
        return null;
    }
    
    
    public void callback(String name) {
        this.getController(name).callback();
    }
    
    public void unloadController(String name) {
        Platform.runLater(() -> {
            if (controllers.remove(name) == null) {
                LOGGER.error("Controller不存在，请检查名称");
            } else {
                LOGGER.info("Controller移除成功");
            }
        });
    }
    
    public void unloadAll() {
        Platform.runLater(() -> controllers.clear());
    }
    
    
}
